﻿#include "useful.h"
//#include <WinDef.h>
#include <fstream>


bool IsTextUTF8CSharp(const std::string& inputStream)
{
    int encodingBytesCount = 0;
    bool allTextsAreASCIIChars = true;

    typedef unsigned char byte;

    for (int i = 0; i < inputStream.length(); i++)
    {
        byte current = inputStream[i];

        if ((current & 0x80) == 0x80)
        {
            allTextsAreASCIIChars = false;
        }
        // First byte
        if (encodingBytesCount == 0)
        {
            if ((current & 0x80) == 0)
            {
                // ASCII chars, from 0x00-0x7F
                continue;
            }

            if ((current & 0xC0) == 0xC0)
            {
                encodingBytesCount = 1;
                current <<= 2;

                // More than two bytes used to encoding a unicode char.
                // Calculate the real length.
                while ((current & 0x80) == 0x80)
                {
                    current <<= 1;
                    encodingBytesCount++;
                }
            }
            else
            {
                // Invalid bits structure for UTF8 encoding rule.
                return false;
            }
        }
        else
        {
            // Following bytes, must start with 10.
            if ((current & 0xC0) == 0x80)
            {
                encodingBytesCount--;
            }
            else
            {
                // Invalid bits structure for UTF8 encoding rule.
                return false;
            }
        }
    }

    if (encodingBytesCount != 0)
    {
        // Invalid bits structure for UTF8 encoding rule.
        // Wrong following bytes count.
        return false;
    }

    // Although UTF8 supports encoding for ASCII chars, we regard as a input stream, whose contents are all ASCII as default encoding.
    return !allTextsAreASCIIChars;
}

//gbk ת utf8
std::string GBKToUTF8(const std::string& strGBK)
{
    std::string strOutUTF8 = "";
    WCHAR * str1;
    int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
    str1 = new WCHAR[n];
    MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
    n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
    char * str2 = new char[n];
    WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
    strOutUTF8 = str2;
    delete[]str1;
    str1 = NULL;
    delete[]str2;
    str2 = NULL;
    return strOutUTF8;
}

//utf-8 ת gbk
std::string UTF8ToGBK(const std::string& strUTF8)
{
    int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
    unsigned short * wszGBK = new unsigned short[len + 1];
    memset(wszGBK, 0, len * 2 + 2);
    MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUTF8.c_str(), -1, (LPWSTR)wszGBK, len);

    len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
    char *szGBK = new char[len + 1];
    memset(szGBK, 0, len + 1);
    WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, (LPSTR)szGBK, len, NULL, NULL);
    //strUTF8 = szGBK;
    std::string strTemp(szGBK);
    delete[]szGBK;
    delete[]wszGBK;
    return strTemp;
}

string getFileName(string fileName)
{

    int pos = fileName.rfind('\\');

    if (pos == -1)
        return fileName;
    else
        return fileName.substr(pos + 1);
}

string getFileNameNoExt(string fileName)
{
    int pos = fileName.rfind('\\');

    if (pos == -1)
        return fileName.substr(0, fileName.find_last_of('.'));
    else
    {
        string s = fileName.substr(pos + 1);
        return s.substr(0, s.find_last_of('.'));
    }
}

string getFileExt(string fileName)
{
    return fileName.substr(fileName.find_last_of('.') + 1);//获取文件后缀;
}


bool delFileLine(string filename, string strLabel,bool isFilter)
{
	//读取文件行内容过滤后保存到字符串数组
	vector<string> vecContent;
	string strLine;
	ifstream inFile(filename);
	if (!inFile.is_open()) return false;
	while (getline(inFile, strLine))
	{
		//过滤
		if ((isFilter&&strLine.rfind(strLabel) == -1) ||(!isFilter&&strLine.rfind(strLabel) == 0))
			vecContent.push_back(strLine);
	}
	inFile.close();

	//覆盖保存
	int vc = vecContent.size();
	if (vc > 0)
	{
		ofstream outFile(filename);

		for (int i = 0; i < vc; ++i)
		{
			string s = vecContent[i];
			if (vc - 1 != i)
				s += "\n";
			outFile << s;//<< endl;
		}
	}

	return true;
}

double getRatio(int index, double dfirst)
{
	if (index < 0) return 0;
	if (index == 0) return dfirst;
	return pow(1 - dfirst, index)*dfirst;
}